#include<stdio.h>
#include<malloc.h>
#include<math.h>

void Display(int *list,int len)
{	
int i ;
for (i=0;i<len;i++)
printf("%d \t",list[i]);
printf("\n");
}

void Insert(int *list,int New,int Pos,int len)
{	
int i;
printf("Enter The Number to be Inserted in the List");
scanf("%d",&New);
printf("Enter The Index in the List where you want to Insert");
scanf("%d",&Pos);
list=realloc(list,(len=len+1)*sizeof(int));
if(Pos<=len && Pos>=0)
{
for(i=len; i>Pos; i--)
{
list[i]=list[i-1];
}
list[Pos]=New;		 
printf("The Element has been Inserted at the required position \n");
Display(list,len);
}
else printf("The Element cannot be Inserted.Invalid Input"); 
}

void Append(int *list,int NE,int len)
{ 
printf("Enter The Number to be appended \n");
scanf("%d",&NE);
int i=0;
list=realloc(list,(len+1)*sizeof(int));
list[len]=NE;
len=len+1;
printf("The Number has been appended \n");
Display(list,len);
}

int Extend(int *list,int *NA,int ex,int len)
{
int i=0,j=0;
printf("Enter the Number of elements you want to extend the list size by \n");
scanf("%d",&ex);
NA=(malloc(ex*sizeof(int)));
printf("Enter the Elements you want to add \n");
for(i=0; i<ex ;i++)
scanf("%d",&NA[i]);
list=realloc(list,(len+ex)*(sizeof(int)));
for(i=len,j=0 ;i<len+ex; i++,j++)
list[i]=NA[j];
len=len+ex;
printf("The list has been Extended as required \n");
Display(list,len);
return ex;
}


void Delete(int *list, int De,int len)
{
int i=0,j,temp;
printf("Enter The Element to be deleted \n");
scanf("%d",&De);
for (i=0; i<len; i++)
if (list[i]==De)
{
temp=i;
break;
}
for(j=temp;j<len-1;j++)
list[j]=list[j+1];
len=len-1;
printf("The Element has been deleted::");
Display(list,len);
}

void Pop(int *list,int Etbp,int len)
{ 	
int i=0;
printf("Enter The Position To Pop ::");
scanf("%d",&Etbp);
if(Etbp<=len)
{
for(i=Etbp; i<len;i++)
{
list[i]=list[i+1];
}
len=len-1;
printf("The Element has been Popped from the given position:: ");
Display(list,len);
}
else 
printf("\nElement Cannot be Popped \n");
}
 

void Index(int *list,int Ntbi,int len)
{	
int Indx=0;
printf("Enter The Number to be Indexed \n");
scanf("%d",&Ntbi);
for (Indx=0; Indx<len; Indx++) 
{
if (list[Indx]==Ntbi)
{
printf("The Index of the number is %d ",Indx);
break;
}
else 
Indx=-1;
}
if(Indx==-1)	
printf("\nNumber Not present in the list \n");
}
	
void Count(int *list,int Ntbc,int len)
{ 
int i=0,cnt=0;
printf("Enter The Number to be Counted \n");
scanf("%d",&Ntbc);	
for (i=0; i<len; i++)
if (list[i]==Ntbc)
cnt++; 
if(cnt!=0)
printf( "The Number is present %d times in the list \n",cnt); 
else
printf("\n Not Present in the list \n ");
}


void Reverse(int *list ,int len)
{
int i,j,rev[len];
for(i=0; i<len; i++)
{
rev[i]=list[len-i-1];
}
for(i=0; i<len; i++)
{
list[i]=rev[i];
printf("%d \t",list[i]);
}
printf("\n");
printf("The List has been reversed \n");
}

void Sort(int *list, int len) 
{

int temp,i,j;
for(i=0;i<=len;i++)
for(j=0;j<=len;j++)
if(list[i]<=list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
printf("The Array has been sorted \n");
Display(list,len);
}  


int main()
{
int i,j;
int *list,New,len,choice,Pos,NE,*NA,ex,De,Ntbc,Ntbi,Etbp;
char ch='y';
printf("Enter Size of the List \n");
scanf("%d",&len);
list=(malloc(len*(sizeof(int))));
printf("Enter the elements of the List \n");
for(i=0; i<len; i++)
scanf("%d",&list[i]);
do
{
printf("1.Insert/2.Append/3.Extend/4.Delete/5.Pop/6.Index/7.Count/8.Sort/9.Reverse/10.Display/11.Quit \n ");
scanf("%d",&choice);
switch(choice)
{
case 1:Insert(list,New,Pos-1,len);
       len=len+1;
       break;
case 2:Append(list,NE,len);
       len=len+1;
       break; 	
case 3:Extend(list,NA,ex,len);
       len=len+ex;	
       break;
case 4:Delete(list,De,len);
       len=len-1;		
       break;
case 5:Pop(list,Etbp,len);
       len=len-1;
       break;
case 6:Index(list,Ntbi,len);
       break;
case 7:Count(list,Ntbc,len);
       break;
case 8:Sort(list,len);
       break;
case 9:Reverse(list,len);
       break;
case 10:Display(list,len);
        break;
case 11:return 0;
	break;

}

printf("You want to Continue Y/N ?");
scanf("%s",&ch);
}while(ch!='N' && ch!='n');
}
